﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EasyCLRBase
{
    /// <summary>
    /// Create a better hash for the objects
    /// </summary>
    public class HashBuilder
    {
        private const int Prime1 = 7;
        private const int Prime2 = 13;
        private int result = Prime1;

        public HashBuilder()
        {
        }

        public HashBuilder(int startHash)
        {
            this.result = startHash;
        }

        public int Result
        {
            get
            {
                return this.result;
            }
        }
        /// <summary>
        /// Build Hash Code
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        public void AddItem<T>(T item)
        {
            unchecked
            {
                this.result = this.result * Prime2 * item.GetHashCode();
            }
        }
        /// <summary>
        /// Add parameters and build a hash code
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        public void AddItems<T>(params T[] items)
        {
            foreach (T item in items)
            {
                this.AddItem(item);
            }
        }

        public static int GetHashCode(params Object[] items)
        {
            var x = new HashBuilder();
            items = (from n in items where n != null select n).ToArray();
            x.AddItems(items);
            return x.Result;
        }
    }
}
